package com.after.saas.file.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.after.saas.common.model.TwoTuple;
import com.after.saas.common.utils.RedisUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Map;

/**
 * 常用枚举
 *
 * @Classname DictUtil
 * @Author hjchen
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2022/11/8 11:22
 */


public class DictUtil {

    private static final String SPLIT = "-";

    public static TwoTuple<String, String> getBaseCode(StringRedisTemplate redisTemplate, String key, String value) {
        String left = "";
        if (StrUtil.isEmpty(value)) {
            return new TwoTuple<>(left, value);
        }
        if (value.indexOf(SPLIT) > 0) {
            value = value.substring(0, value.indexOf("-"));
        }
        Object cacheValue = RedisUtil.getObject(redisTemplate, key);
        if (ObjectUtil.isNotEmpty(cacheValue)) {
            JSONObject object = JSONUtil.parseObj(cacheValue);
            List<Map> maps = JSONUtil.toList(object.getJSONArray(key), Map.class);
            if (CollectionUtil.isNotEmpty(maps)) {
                for (Map<String, String> map : maps) {
                    String mapKey = map.get("code");
                    String mapValue = map.get("nameCn");
                    if (StrUtil.equalsAnyIgnoreCase(value, mapKey, mapValue)) {
                        left = mapKey;
                        break;
                    }
                }
            }
        }
        return new TwoTuple<>(left, value);
    }

    public static TwoTuple<String, String> getRemoteCode(StringRedisTemplate redisTemplate, String key, String value) {
        String left = "";
        if (StrUtil.isEmpty(value)) {
            return new TwoTuple<>("", value);
        }
        if (value.indexOf(SPLIT) > 0) {
            value = value.substring(0, value.indexOf("-"));
        }
        Object cacheValue = RedisUtil.getObject(redisTemplate,key);
        if (ObjectUtil.isNotEmpty(cacheValue)) {
            JSONObject object = JSONUtil.parseObj(cacheValue);
            List<Map> maps = JSONUtil.toList(object.getJSONArray(key), Map.class);
            if (CollectionUtil.isNotEmpty(maps)) {
                for (Map<String, String> map : maps) {
                    String gbCode = map.get("gbCode");
                    String codeValue = map.get("codeValue");
                    String cusCode = map.getOrDefault("cusCode", "");
                    String mapKey = StrUtil.isEmpty(gbCode) ? codeValue : gbCode;
                    String mapValue = map.get("codeName");
                    if (StrUtil.equalsAnyIgnoreCase(value, mapKey, mapValue, cusCode)) {
                        left = mapKey;
                        break;
                    }
                }
            }
        }
        return new TwoTuple<>(left, value);
    }

}
